package cn.xm.lingshi.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.xm.lingshi.pojo.AlipayBean;
import cn.xm.lingshi.pojo.GouWuChe;
import cn.xm.lingshi.pojo.YongHu;
import cn.xm.lingshi.service.CartService;
import cn.xm.lingshi.service.PayService;
import cn.xm.lingshi.service.impl.CartServiceImpl;
import cn.xm.lingshi.service.impl.PayServiceImpl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;


public class PayServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		CartService order=new CartServiceImpl();
		YongHu user=(YongHu)request.getSession().getAttribute("denglu");
		String id="0";
		GouWuChe list= null;
		if(user!=null){
			list= order.queryRecord(0,10, user.getUserId());
			
		}
		
		//调用支付服务
		PayService payService=new PayServiceImpl();
		System.out.println(list.getCartid());
		String out_trade_no=String.valueOf(list.getCartid());
		String subject="星梦食品";
		System.out.println(list.getMoney());
		String total_amount=String.valueOf(list.getMoney());
		String body="好吃不贵，便宜实惠";
		
		
		AlipayBean bean=new AlipayBean(out_trade_no,subject,
				new StringBuffer().append(total_amount),body);
		bean.setTimeout_express("1c");

		try {
			out.print(payService.aliPay(bean));
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
	
		
		out.flush();
		out.close();
	}

}
